package com.djzhou.gmms.service.company.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;

import com.djzhou.gmms.model.company.TbCompany;
import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.gmms.service.base.impl.BaseServiceImpl;
import com.djzhou.gmms.service.company.CompanyService;
import com.djzhou.util.DateUtil;

public class CompanyServiceImpl  extends BaseServiceImpl implements CompanyService{

	@Override
	public void add(TbCompany t) {
		companyDAO.insert(t);
	}

	@Override
	public void delete(Integer id) {
		TbCompany company=companyDAO.queryByID(TbCompany.class, id);
		company.setDeleteFlag(1);
		companyDAO.update(company);
	}

	@Override
	public TbCompany queryById(Integer id) {
		return companyDAO.queryByID(TbCompany.class, id);
	}

	@Override
	public List<TbCompany> queryList(HashMap<String, Object> hashMap) {
		String hql="from TbCompany company where company.deleteFlag=0 and company.companyId<>1";
		return companyDAO.query(hql);
	}

	@Override
	public PageBean<TbCompany> queryPageBean(HashMap<String, Object> hashMap,
			int pageSize, int currentPage) {
		String startDate=null;
		String endDate=null;
		String companyNo=null;
		String companyName=null;
		String hql="";
		if(hashMap.get("companyId").equals(1))
			hql="from TbCompany company where company.deleteFlag=0 and company.companyId<>1";
		else
			hql="from TbCompany company where company.deleteFlag=0 and company.companyId="+hashMap.get("companyId").toString();
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("companyNo")!=null){
				companyNo=hashMap.get("companyNo").toString();
				sb.append("and company.companyNo like '%").append(companyNo).append("%'");
			}
			if(hashMap.get("companyName")!=null){
				companyName=hashMap.get("companyName").toString();
				sb.append(" and company.companyName like '%").append(companyName).append("%'");
			}
			
			if(hashMap.get("startDate")!=null){
				startDate=hashMap.get("startDate").toString();
//				Date date1=DateUtil.turnStringToDate(startDate, "yyyy-MM-dd");
//				date1=DateUtil.getBeforeDate(date1, 1);
//				startDate=DateUtil.turnDateToString(date1, "yyyy-MM-dd");
				sb.append(" and company.createTime>='").append(startDate).append("'");
			}
			if(hashMap.get("endDate")!=null){
				endDate=hashMap.get("endDate").toString();
//				Date date2=DateUtil.turnStringToDate(endDate, "yyyy-MM-dd");
//				date2=DateUtil.getAfterDate(date2, 1);
//				endDate=DateUtil.turnDateToString(date2, "yyyy-MM-dd");
				sb.append(" and company.createTime<='").append(endDate).append("'");
			}
		}
		sb.append(" order by createTime desc");
		hql=sb.toString();
		PageBean<TbCompany> pageBean=companyDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public void update(TbCompany t) {
		companyDAO.update(t);
	}

	@Override
	public List<TbCompany> queryList(Integer companyId) {
		String hql="from TbCompany where deleteFlag=0 and companyId<>? and companyId<>1";
		return companyDAO.query(hql,companyId);
	}
	@Override
	public VoResource query4AutoComplete(HashMap<String, Object> hashMap, String sidx,
			String sord, int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TbCompany> queryChildList(Integer parentCompanyId) {
		String hql="from TbCompany company where company.parentCompanyId=? and deleteFlag=0";
		return companyDAO.query(hql,parentCompanyId);
	}

	@Override
	public List<TbCompany> queryAllChildList(Integer parentCompanyId) {
		String hql="from TbCompany company where company.companyPath like'%,"+parentCompanyId+",%'";
		return companyDAO.query(hql);
	}

}
